package com.apress.prospring5.ch7.base;

import com.apress.prospring5.ch7.base.config.AppConfig;
import com.apress.prospring5.ch7.base.dao.SingerDao;
import com.apress.prospring5.ch7.base.entities.Album;
import com.apress.prospring5.ch7.base.entities.Instrument;
import com.apress.prospring5.ch7.base.entities.Singer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.GenericApplicationContext;

import java.util.List;

public class SpringHibernateDemo {
    private static Logger logger= LoggerFactory.getLogger(SpringHibernateDemo.class);

    public static void main(String[] args) {
        GenericApplicationContext ctx=new AnnotationConfigApplicationContext(AppConfig.class);
        SingerDao singerDao=ctx.getBean(SingerDao.class);
        listSingers(singerDao.findAll());
        ctx.close();
    }

    private static void listSingers(List<Singer> singers){
        logger.info("---- Listing singers:");
        for(Singer singer:singers){
            logger.info(singer.toString());
        }
    }

    private static void listSingersWithAlbum(List<Singer> singers){
        logger.info("----Listing singers with instruments:");
        for(Singer singer:singers){
            logger.info(singer.toString());
            if(singer.getAlbums()!=null){
                for(Album album:singer.getAlbums()){
                    logger.info("\t"+album.toString());
                }
            }

            if(singer.getInstruments()!=null){
                for(Instrument instrument:singer.getInstruments()){
                    logger.info("\t"+instrument.getInstrumentId());
                }
            }
        }
    }
}
